package com.example.ListItems;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.andoidepubziputil.R;

import android.app.Activity;
import android.widget.Filter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class mArrAdapter extends ArrayAdapter<mListItem> implements Filterable {

	private Activity acti;
	List<mListItem> Orginal;
	float Tsize = 0;
	List<mListItem> filtered;

	public mArrAdapter(Activity context, List<mListItem> objects, float Tsize) {
		super(context, R.layout.list_item, objects);
		this.acti = context;
		this.Orginal = objects;
		this.filtered = objects;
		this.Tsize = Tsize;

	}

	public static class mViewHolder {
		public TextView Ten_sach;
		public TextView Dung_luong;
		public ImageView hinhanh;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return filtered.size();
	}

	@Override
	public mListItem getItem(int position) {
		// TODO Auto-generated method stub
		return filtered.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = convertView;

		if (convertView == null) {
			mViewHolder holder = new mViewHolder();
			LayoutInflater inflater = this.acti.getLayoutInflater();
			row = inflater.inflate(R.layout.list_item, null);
			holder.Ten_sach = (TextView) row.findViewById(R.id.Ten_sach);
			holder.Dung_luong = (TextView) row.findViewById(R.id.Dung_luong);
			holder.hinhanh = (ImageView) row.findViewById(R.id.hinhanh);
			row.setTag(holder);
		}
		mViewHolder mViewHolder = new mViewHolder();
		mViewHolder = (mViewHolder) row.getTag();
		mViewHolder.Ten_sach.setText(filtered.get(position).Ten_sach);
		mViewHolder.Dung_luong.setText(filtered.get(position).Dung_luong);
		mViewHolder.hinhanh.setImageBitmap(filtered.get(position).hinhanh);
		if (Tsize != 0)
			mViewHolder.Ten_sach.setTextSize(Tsize);
		return row;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return new Filter() {

			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults filterResults) {
				filtered = (ArrayList<mListItem>) filterResults.values;
				notifyDataSetChanged();

			}

			@Override
			protected FilterResults performFiltering(CharSequence charSequence) {
				FilterResults results = new FilterResults();

				// If there's nothing to filter on, return the original data for
				// your list
				if (charSequence == null || charSequence.length() == 0) {
					results.values = Orginal;
					results.count = Orginal.size();
				} else {

					ArrayList<mListItem> filterResultsData = new ArrayList<mListItem>();

					for (mListItem data : Orginal) {

						// In this loop, you'll filter through originalData and
						// compare each item to charSequence.
						// If you find a match, add it to your new ArrayList
						// I'm not sure how you're going to do comparison, so
						// you'll need to fill out this conditional

						if (data.Ten_sach.toUpperCase().contains(
								charSequence.toString().toUpperCase())) {
							filterResultsData.add(data);
						}
					}

					results.values = filterResultsData;
					results.count = filterResultsData.size();
				}

				return results;
			}
		};
	}
}
